package com.linghui.videoplus.ipai.activity;

import com.linghui.videoplus.ipai.R;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;


public class HomeActivity extends TabActivity implements OnCheckedChangeListener{
	private RadioGroup mainTab;
	private TabHost mTabHost;
	
	//内容Intent
	private Intent mBrowserIntent;
	private Intent mCameraIntent;
	private Intent mPhotoIntent;
	
	private final static String TAB_TAG_HOME="tab_tag_home";
	private final static String TAB_TAG_CAMERA="tab_tag_camera";
	private final static String TAB_TAG_PHOTO="tab_tag_photo";
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_home);
        mainTab=(RadioGroup)findViewById(R.id.main_tab);
        mainTab.setOnCheckedChangeListener(this);
        prepareIntent();
        setupIntent();
    }
    /**
     * 准备tab的内容Intent
     */
	private void prepareIntent() {
		mBrowserIntent=new Intent(this, BrowserActivity.class);
		mCameraIntent=new Intent(this, CameraActivity.class);
		mPhotoIntent=new Intent(this, VideoCascadeActivity.class);
	}
	/**
	 * 
	 */
	private void setupIntent() {
		this.mTabHost=getTabHost();
		TabHost localTabHost=this.mTabHost;
		localTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.menu_hot, R.drawable.btn_hot_up, mBrowserIntent));
		localTabHost.addTab(buildTabSpec(TAB_TAG_CAMERA, R.string.menu_camera, R.drawable.camera_up, mCameraIntent));
		localTabHost.addTab(buildTabSpec(TAB_TAG_PHOTO, R.string.menu_photo, R.drawable.btn_photo_up, mPhotoIntent));
	}
	/**
	 * 构建TabHost的Tab页
	 * @param tag 标记
	 * @param resLabel 标签
	 * @param resIcon 图标
	 * @param content 该tab展示的内容
	 * @return 一个tab
	 */
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
		return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	} 
	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		if(checkedId==R.id.radio_hot){
			this.mTabHost.setCurrentTabByTag(TAB_TAG_HOME);
		}else if(checkedId==R.id.radio_photo){
			this.mTabHost.setCurrentTabByTag(TAB_TAG_PHOTO);
		}
	}
	
	public static void launch(Activity mActivity) {
		Intent intent = new Intent(mActivity, HomeActivity.class);
		mActivity.startActivity(intent);
		mActivity.finish();
	}
    
}